package zhanyuduilie

import (
	"fmt"
	"strconv"
)

func EvalRPN(tokens []string) int {
	// 还是属于栈的用法
	luoji := map[string]int{"+": 1, "-": 1, "*": 1, "/": 1}
	ans := 0
	stack := make([]string, 0)
	for _, token := range tokens {
		if luoji[token] == 1 && len(stack) >= 2 {
			a, _ := strconv.Atoi(stack[len(stack)-2])
			b, _ := strconv.Atoi(stack[len(stack)-1])
			stack = stack[:len(stack)-2]
			x := ""
			switch token {
			case "+":
				x = strconv.Itoa(a + b)
			case "-":
				x = strconv.Itoa(a - b)
			case "*":
				x = strconv.Itoa(a * b)
			case "/":
				x = strconv.Itoa(a / b)
			default:

			}
			stack = append(stack, x)
		} else {
			stack = append(stack, token)
		}
	}
	ans, _ = strconv.Atoi(stack[0])
	fmt.Println(ans)
	return ans
}
